package com.disruptor.demo.util;

import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:HelloHeSir@gmail.com">Mr_He</a>
 * @Copyright (c)</ b> HeC<br/>
 * @createTime 2018/7/27 10:30
 * @Description:线程池
 */
public class ThreadPool {

    private static final String THREAD_NAME = "Lend-Thread-";

    private static final Integer POOL_MIN = 100;

    private static final Integer POOL_SIZE = 100;

    private static ThreadPoolExecutor threadPool;

    static {

        final ThreadGroup group = Thread.currentThread().getThreadGroup();


        threadPool = new ThreadPoolExecutor(POOL_MIN, POOL_SIZE,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(group,r,THREAD_NAME+nextThreadNum());
            }
        });
    }

    private static Long threadNum = 0L;

    private static synchronized String nextThreadNum() {
        return (++threadNum)+"";
    }

    /**
     * 运行线程
     * @param runnable 传入一个new好的线程
     */
    public static void exe(Runnable runnable){
        threadPool.execute(runnable);
    }

}